<?php
/**
 * Created by PhpStorm.
 * User: xi
 * Date: 2020-05-02
 * Time: 16:54
 */

namespace app\api\service;


use app\admin\model\Assistantprofit;
use app\admin\model\Shop;
use app\admin\model\Shopbalanceprofit;
use app\admin\model\Shoporderprofit;
use app\admin\model\Userorderprofit;
use app\admin\model\Userwithdraw;

class BalanceService
{


    /**
     * 店铺用户总余额
     */
    public function shopBalance($user_id){

        $shopOrderProfitModel = new Shoporderprofit();
        $totalProfit = $shopOrderProfitModel->where('user_id',$user_id)->sum('amount');

        $userWithDrawModel = new Userwithdraw();
        $withAmount = $userWithDrawModel->where('user_id',$user_id)->where('type','shop')->sum('amount');

        $shopModel = new Shop();
        $shopList = $shopModel->where('user_id',$user_id)->select();
        $ids = array();
        foreach ($shopList as $key=>$val){
            $ids[] = $val['id'];
        }
        $shopBalanceProfitModel = new Shopbalanceprofit();
        $balanceProfit = $shopBalanceProfitModel->where('shop_id','in',$ids)->sum('amount');
        $balance = $totalProfit-$withAmount+$balanceProfit;

        return formatMoney($balance);
    }

    public function shopTodayProfit($user_id){
        $shopOrderProfitModel = new Shoporderprofit();
        $todayProfit = $shopOrderProfitModel->whereTime('create_time','today')->where('user_id',$user_id)->sum('amount');
        $shopModel = new Shop();
        $shopList = $shopModel->where('user_id',$user_id)->select();
        $ids = array();
        foreach ($shopList as $key=>$val){
            $ids[] = $val['id'];
        }
        $shopBalanceProfitModel = new Shopbalanceprofit();
        $balanceProfit = $shopBalanceProfitModel->where('shop_id','in',$ids)->whereTime('create_time','today')->sum('amount');

        return $todayProfit+$balanceProfit;
    }


    public function shopMonthProfit($user_id){
        $shopOrderProfitModel = new Shoporderprofit();
        $monthProfit = $shopOrderProfitModel->whereTime('create_time','month')->where('user_id',$user_id)->sum('amount');

        return $monthProfit;
    }
    public function shopLastMonthProfit($user_id){
        $shopOrderProfitModel = new Shoporderprofit();
        $lastProfit = $shopOrderProfitModel->whereTime('create_time','last month')->where('user_id',$user_id)->sum('amount');

        return $lastProfit;
    }



    /**
     * 店铺用户总余额
     */
    public function agentBalance($user_id){

        $shopOrderProfitModel = new Userorderprofit();
        $totalProfit = $shopOrderProfitModel->where('user_id',$user_id)->sum('amount');

        $userWithDrawModel = new Userwithdraw();
        $withAmount = $userWithDrawModel->where('user_id',$user_id)->where('type','user')->sum('amount');

        $balance = $totalProfit-$withAmount;
        return formatMoney($balance);
    }

    public function agentTodayProfit($user_id){
        $shopOrderProfitModel = new Userorderprofit();
        $todayProfit = $shopOrderProfitModel->whereTime('create_time','today')->where('user_id',$user_id)->sum('amount');

        return $todayProfit;
    }


    public function agentMonthProfit($user_id){
        $shopOrderProfitModel = new Userorderprofit();
        $monthProfit = $shopOrderProfitModel->whereTime('create_time','month')->where('user_id',$user_id)->sum('amount');

        return $monthProfit;
    }
    public function agentLastMonthProfit($user_id){
        $shopOrderProfitModel = new Userorderprofit();
        $lastProfit = $shopOrderProfitModel->whereTime('create_time','last month')->where('user_id',$user_id)->sum('amount');

        return $lastProfit;
    }

    public function assTodayProfit($user_id)
    {

        $assistantProfitModel = new Assistantprofit();
        $profit = $assistantProfitModel->where('user_id',$user_id)->whereTime('create_time','today')->sum('amount');
        return $profit;
    }

    public function assMonthProfit($user_id)
    {

        $assistantProfitModel = new Assistantprofit();
        $profit = $assistantProfitModel->whereTime('create_time','month')->where('user_id',$user_id)->sum('amount');
        return $profit;
    }

    public function assBalance($user_id){

        $assistantProfitModel = new Assistantprofit();
        $totalProfit = $assistantProfitModel->where('user_id',$user_id)->sum('amount');

        $userWithDrawModel = new Userwithdraw();
        $withAmount = $userWithDrawModel->where('user_id',$user_id)->where('type','assistant')->sum('amount');

        $balance = $totalProfit-$withAmount;
        return formatMoney($balance);
    }

    public function assEnableBalance($user_id){

        $assistantProfitModel = new Assistantprofit();
        $totalProfit = $assistantProfitModel->where('user_id',$user_id)->where('status',1)->sum('amount');

        $userWithDrawModel = new Userwithdraw();
        $withAmount = $userWithDrawModel->where('user_id',$user_id)->where('type','assistant')->sum('amount');

        $balance = $totalProfit-$withAmount;
        return formatMoney($balance);
    }

    public function assLastMonthProfit($user_id)
    {

        $assistantProfitModel = new Assistantprofit();
        $profit = $assistantProfitModel->whereTime('create_time','last month')->where('user_id',$user_id)->sum('amount');
        return $profit;
    }

    public function assWithProfit($user_id)
    {
        $assistantProfitModel = new Assistantprofit();
        $profit = $assistantProfitModel->where('status',1)->where('user_id',$user_id)->sum('amount');
        return $profit;
    }

}